package com.example.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.Service.CgOrderService;
import com.example.dao.CgOrderMapper;
import com.example.exception.MyException;
import com.example.pojo.CgOrder;
import com.example.util.Result;
import com.example.util.ThreadLocalUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/cgorder")
@Api(tags = "采购")
public class CgOrderController {
    @Resource
    private CgOrderMapper cgOrderMapper;
    @Resource
    private CgOrderService cgOrderService;

//  根据id查询采购请求信息
@GetMapping("/getList")
public Object getList(){
    System.out.println( ThreadLocalUtil.getCurrentId());
    QueryWrapper<CgOrder>
            queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("create_by", ThreadLocalUtil.getCurrentId());
    List<CgOrder> cgOrderList=cgOrderService.list(queryWrapper);

    return new Result<>("200","查询成功",cgOrderList);
 }


//  添加采购
 @PostMapping("/add")
    public Object add(@RequestBody CgOrder cgOrder){
    cgOrder.setTotal(cgOrder.getPrice()* cgOrder.getQuantity());
     System.out.println(cgOrder.getTotal());
    if (!cgOrderService.save(cgOrder)) {
        throw new MyException("459","添加采购信息失败");
    }
     return new Result<>("200","添加采购信息成功");
 }

//  撤销采购
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("id") Integer id){
//                逻辑删除
        if (!cgOrderService.removeById(id)){
            throw new MyException("460","撤销采购信息失败");
        }
        return new Result<>("200","撤销采购信息成功");
    }







}
